package admin.code.generator.core.metadata;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import admin.code.generator.util.ConfigUtils;
import admin.code.generator.util.ConnectionManager;
import admin.code.generator.util.StringUtil;
import freemarker.log.Logger;

/**
 *
 *@author Jack
 *2016年3月4日下午10:30:44
 */
public class TableMetaData {

	private static Logger log = Logger.getLogger(TableMetaData.class.getName());

	private String tableName;

	private List<ColumnMetaData> columns = null;

	private String idName;
	private String idType;

	public TableMetaData(String tableName) {
		this.tableName = tableName;
		init();
	}

	private void init() {
		Connection con = null;
		try {
			//			String url = ConfigUtils.get("db.url");
			//			String user = ConfigUtils.get("db.user");
			//			String password = ConfigUtils.get("db.pwd");
			//			con = ConnectionManager.getConnection(url,user,password);
			con = ConnectionManager.getConnection();
			DatabaseMetaData dbmd = con.getMetaData();
			//获取主键
			ResultSet rs = dbmd.getPrimaryKeys("", "", tableName);
			String primaryKeyName = "";
			while (rs.next()) {
				primaryKeyName = rs.getString("COLUMN_NAME");
			}
			idName = primaryKeyName;
			//获取所有列
			ResultSet colRet = dbmd.getColumns(null, "%", tableName, "%");
			columns = new ArrayList<ColumnMetaData>();
			while (colRet.next()) {
				String columnName = colRet.getString("COLUMN_NAME");
				String columnType = colRet.getString("TYPE_NAME");
				String columnComment = colRet.getString("REMARKS");
				int datasize = colRet.getInt("COLUMN_SIZE");

				boolean isPrimaryKey = StringUtil.isNotBlank(primaryKeyName) && primaryKeyName.equals(columnName);
				if (isPrimaryKey) {
					idType = columnType;
				}
				columns.add(new ColumnMetaData(columnName, columnType, columnComment, datasize, isPrimaryKey));
			}

		} catch (Exception e) {
			log.info("读取数据库表结构失败!" + tableName + "");
			log.error(e.getMessage(), e);
			System.exit(0);// 直接退出 
		} finally {
			if (con != null) {
				try {
					con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

	public void setIdType(String idType) {
		this.idType = idType;
	}

	public String getIdType() {
		return idType;
	}

	public String getIdName() {
		return idName;
	}

	public void setIdName(String idName) {
		this.idName = idName;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public List<ColumnMetaData> getColumns() {
		return columns;
	}

	public void setColumns(List<ColumnMetaData> columns) {
		this.columns = columns;
	}

	public static void main(String[] args) throws Exception {
		TableMetaData tmd = new TableMetaData("t_order");
		System.err.println(tmd.getColumns());
		System.err.println("t_game".subSequence(2, "t_game".length()));
	}
}
